﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WgrusSetup.Tools;

namespace WgrusSetup
{
    public partial class Form1 : Form
    {
        private ConnectionStringEditor connectionStringEditor;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            connectionStringEditor = new ConnectionStringEditor();
        }

        private void bt_UpdateSettings_Click(object sender, EventArgs e)
        {
            string where="";
            try
            {
                where = "AccountingSetup";
                AccountingSetup();
                where = "CallCenterSetup";
                CallCenterSetup();
                where = "WidgetSetup";
                WidgetSetup();
                where = "GadgetSetup";
                GadgetSetup();
                MessageBox.Show("Config update succes", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ee)
            {

                MessageBox.Show("Config update failed at: " + where + ". Message: " + ee.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            
        }

        private void AccountingSetup()
        {
            connectionStringEditor.LoadConfig(txt_Accounting_ConfigPath.Text);
            connectionStringEditor.UpdateEntityFrameworkConnection(txt_Accounting_SqlServer.Text, txt_Accounting_Database.Text);
            connectionStringEditor.SaveConfig(txt_Accounting_ConfigPath.Text);
        }

        private void CallCenterSetup()
        {
            connectionStringEditor.LoadConfig(txt_CallCenter_ConfigPath.Text);
            connectionStringEditor.UpdateEntityFrameworkConnection(txt_CallCenter_SqlServer.Text, txt_CallCenter_Database.Text);
            connectionStringEditor.SaveConfig(txt_CallCenter_ConfigPath.Text);
        }

        private void WidgetSetup()
        {
            connectionStringEditor.LoadConfig(txt_Widget_ConfigPath.Text);
            connectionStringEditor.UpdateEntityFrameworkConnection(txt_Widget_SqlServer.Text, txt_Widget_Database.Text);
            connectionStringEditor.SaveConfig(txt_Widget_ConfigPath.Text);
        }

        private void GadgetSetup()
        {
            connectionStringEditor.LoadConfig(txt_Gadget_ConfigPath.Text);
            connectionStringEditor.UpdateEntityFrameworkConnection(txt_Gadget_SqlServer.Text, txt_Gadget_Database.Text);
            connectionStringEditor.SaveConfig(txt_Gadget_ConfigPath.Text);
        }

        private void bt_Accounting_ConfigPath_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if(dr == DialogResult.OK)
            txt_Accounting_ConfigPath.Text = openFileDialog1.FileName;
        }

        private void bt_CallCenter_ConfigPath_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            txt_CallCenter_ConfigPath.Text = openFileDialog1.FileName;
        }

        private void bt_Widget_ConfigPath_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
                txt_Widget_ConfigPath.Text = openFileDialog1.FileName;
        }

        private void bt_Gadget_ConfigPath_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
                txt_Gadget_ConfigPath.Text = openFileDialog1.FileName;
        }
    }
}
